package com.sojpt.boot.controller.business;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import com.jfinal.aop.Before;
import com.jfinal.kit.Kv;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.SqlPara;
import com.sojpt.boot.controller.business.validator.LkyDocumentTableManageAddValidator;
import com.sojpt.boot.controller.business.validator.LkyDocumentTableManageUpdateValidator;
import com.sojpt.boot.intercepter.admin.AdminLoginInterceptor;
import com.sojpt.boot.intercepter.admin.AdminPopedomInterceptor;
import com.sojpt.boot.ui.iview.IViewController;
import com.sojpt.kit.SoJLogKit;
import com.sojpt.model.DicPopedom;
import com.sojpt.model.LkyCollege;
import com.sojpt.model.LkyDocumentTableManage;
import com.sojpt.model.LkyMajor;
import com.sojpt.util.ResultUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 *
 * @author 张君瑞
 *
 */
@RestController
@RequestMapping("/admin/lkyDocumentTableManage/")
public class LkyDocumentTableManageAdminController extends IViewController {

    @Value("${file.uploadFolder}")
    private String uploadFolder;

    @GetMapping("list")
    public void list() {

        LkyDocumentTableManage lkyDocumentTableManage = getModel(LkyDocumentTableManage.class, "lkyDocumentTableManage");
        lkyDocumentTableManage.put("start_time_start_time", getPara("start_time_start_time"));
        lkyDocumentTableManage.put("start_time_end_time", getPara("start_time_end_time"));
        lkyDocumentTableManage.put("end_time_start_time", getPara("end_time_start_time"));
        lkyDocumentTableManage.put("end_time_end_time", getPara("end_time_end_time"));
        lkyDocumentTableManage.put("update_date_start_time", getPara("update_date_start_time"));
        lkyDocumentTableManage.put("update_date_end_time", getPara("update_date_end_time"));

        SqlPara sqlPara = Db.getSqlPara("lky_document_table_manage.select", getKv(lkyDocumentTableManage));
        Page<Record> page = Db.paginate(pageNumber(), pageSize(), sqlPara);
        renderJson(page);
    }

    @PostMapping("save")
    @Before(LkyDocumentTableManageAddValidator.class)
    public void save() {
        LkyDocumentTableManage lkyDocumentTableManage = getModel(LkyDocumentTableManage.class, "lkyDocumentTableManage");
        DicPopedom dicPopedom = new DicPopedom();
        dicPopedom.setForwardAction("/admin/lkyDocumentTableManage/save");
        if(lkyDocumentTableManage.save()) {
            renderJson(Ret.ok());
            dicPopedom.setPopedomName("添加成功");
        } else {
            renderJson(Ret.fail());
            dicPopedom.setPopedomName("添加失败");
        }
        SoJLogKit.writeLog(this, dicPopedom,getSessionUser());
    }

    @PostMapping("update")
    @Before(LkyDocumentTableManageUpdateValidator.class)
    public void update() {
        LkyDocumentTableManage lkyDocumentTableManage = getModel(LkyDocumentTableManage.class, "lkyDocumentTableManage");
        DicPopedom dicPopedom = new DicPopedom();
        dicPopedom.setForwardAction("/admin/lkyDocumentTableManage/update");
        if(lkyDocumentTableManage.update()) {
            renderJson(Ret.ok());
            dicPopedom.setPopedomName("修改成功");
        } else {
            renderJson(Ret.fail());
            dicPopedom.setPopedomName("修改失败");
        }
        SoJLogKit.writeLog(this, dicPopedom,getSessionUser());
    }

    @PostMapping("delete")
    public void delete() {
        LkyDocumentTableManage lkyDocumentTableManage = getModel(LkyDocumentTableManage.class, "lkyDocumentTableManage");
        DicPopedom dicPopedom = new DicPopedom();
        dicPopedom.setForwardAction("/admin/lkyDocumentTableManage/delete");
        if(LkyDocumentTableManage.dao.deleteById(lkyDocumentTableManage.getId())) {
            renderJson(Ret.ok());
            dicPopedom.setPopedomName("删除成功");
        } else {
            renderJson(Ret.fail());
            dicPopedom.setPopedomName("删除失败");
        }
        SoJLogKit.writeLog(this, dicPopedom,getSessionUser());
    }

    @PostMapping("batchDelete")
    public void batchDelete() {
        DicPopedom dicPopedom = new DicPopedom();
        dicPopedom.setForwardAction("/admin/lkyDocumentTableManage/batchDelete");
        String[] ids = getPara("ids").split(",");
        SqlPara sqlPara = Db.getSqlPara("lky_document_table_manage.deleteByIds", Kv.by("idList", Arrays.asList(ids)));
        if(Db.update(sqlPara) > 0) {
            renderJson(Ret.ok());
            dicPopedom.setPopedomName("批量删除成功");
        } else {
            renderJson(Ret.fail());
            dicPopedom.setPopedomName("批量删除失败");
        }
        SoJLogKit.writeLog(this, dicPopedom,getSessionUser());
    }


    @PostMapping("queryFName")
    public void queryFName() {
        String table_type = getPara("table_type");
        LkyDocumentTableManage lkyDocumentTableManage = new LkyDocumentTableManage();
        lkyDocumentTableManage.setTableType(Integer.valueOf(table_type));
        SqlPara sqlPara = Db.getSqlPara("lky_document_table_manage.select", getKv(lkyDocumentTableManage));
        List<Record> records = Db.find(sqlPara);
        renderJson(ResultUtil.success(records));
    }
}